<?php
/**
 * 项目列表 todo
 * @desc 实现项目的添加，管理，修改，删除等操作
 */
class Api_App_Project extends PhalApi_Api {

    public function getRules() {
        return [
            'add'       => [
                'pname'       => ['name' => 'pname', 'type' => 'string', 'require' => true, 'desc' => '项目名称'],
                'pid'   => ['name' => 'pid', 'type' => 'string', 'require' => true, 'desc' => '项目 pid'],
                'porg'       => ['name' => 'porg', 'type' => 'string', 'require' => true, 'desc' => '所属公司名称'],
                'pdesc'        => ['name' => 'pdesc', 'type' => 'string', 'require' => true, 'desc' => '公司描述'],
                'admin'        => ['name' => 'admin', 'type' => 'string', 'require' => true, 'desc' => '所属的客户的 openid'],
            ],
            'edit'      => [
                'pname'       => ['name' => 'pname', 'type' => 'string', 'require' => true, 'desc' => '项目名称'],
                'pid'   => ['name' => 'pid', 'type' => 'string', 'require' => true, 'desc' => '项目 pid'],
                'porg'       => ['name' => 'porg', 'type' => 'string', 'require' => true, 'desc' => '所属公司名称'],
                'pdesc'        => ['name' => 'pdesc', 'type' => 'string', 'require' => true, 'desc' => '公司描述'],
                'admin'        => ['name' => 'admin', 'type' => 'string', 'require' => true, 'desc' => '所属的客户的 openid'],
            ],
            'Plists'     => [
                'admin'  => ['name' => "admin", 'type' => 'string', 'require' => true, 'desc' => '所属客户的 (openid)'],
            ],
            'delete'    => [
                'pid'    => ['name' => 'pid', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '项目的  pid'],
            ],
            'info'      => [
                'pid' => ['name' => 'pid', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '项目 pid'],
            ],
            'index'      => [
                'pid' => ['name' => 'pid', 'type' => 'string', 'min' => 1, 'require' => true, 'desc' => '项目 pid'],
            ]

        ];
    }
    /**
     * 创建数据库
     * @desc
     */
    public function index(){
        $con = mysql_connect("localhost","root","123456");
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        mysql_select_db("apply", $con);
        $sql = "CREATE TABLE toupiao
                (
                FirstName varchar(15),
                LastName varchar(15),
                Age int
                )";
        mysql_query($sql,$con);

        mysql_close($con);
    }
    /**
     * 根据 pid 获取项目资料 todo
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int pid 项目pid
     */

    public function Info() {
        $data = [
            'pid' => $this->pid,
        ];
        $db = new Domain_App_Project();
        return $db->getProjectByPid($data['pid']);
    }

    /**
     * 根据 admin (openid) 获取客户对应的项目列表
     * @desc
     */

    public function Plists(){
        $data = [
            'admin' =>$this->admin,
        ];
        $db = new Domain_App_Project();
        return $db->plists($data);
    }

    /**
     * 项目添加
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return string msg 提示信息
     */
    public function add() {
        $domain = new Domain_App_Project();
        $data = [
            'ctime'   => DI()->helper->getTime(),
            'ip'      => DI()->helper->getClientIp(),
            "pname"       => $this->pname,
            'pid'         => $this->pid,
            'porg'        => $this->porg,
            'pdesc'       => $this->pdesc,
            'admin'       => $this->admin,
        ];
        $info = $domain->addProject($data);
        if($info){
            return [
                'message' => '添加成功',
            ];
        }
        return [
            'message' => '添加失败',
        ];
    }


    /**
     * 根据 pid 删除一个项目
     * @desc
     */
    public function delete() {
        $db = new Domain_App_Project();
        $data = [
            'pid' => $this->pid,
        ];
        $info = $db->deleteProjectByPid($data);
        if($info){
            return [
                'message' => '删除成功',
            ];
        }
        return [
            'message' => '删除失败',
        ];
    }

    /**
     * 更新项目资料
     * @desc
     * @return int ret HTTP状态码,200成功,其他失败
     * @return int code
     * @return boolean data.isUpdated 是否更新成功
     * @return string msg 提示信息
     */
    public function edit() {
        $db = new Domain_App_Project();
        $data = [
            'ctime'   => DI()->helper->getTime(),
            'ip'      => DI()->helper->getClientIp(),
            'pname'   => $this->pname,
            'pid'     => $this->pid,
            'porg'    => $this->porg,
            'pdesc'   => $this->pdesc,
            'admin'   => $this->admin,
        ];
        $info = $db->editProjectByPid($data,$data['pid']);
        if($info){
            return array(
                'message' => '更新成功',
            );
        }
        return array(
            'message' => '更新失败',
        );

    }

}
